import Vue from 'vue'
import {
  login,
  getInfo,
  logout
} from '@/api/login'

import {
  userList,
  userUpdate
} from '@/api/user'
import {
  USER_KEY,
  ADMIN_ID,
  LOGIN_NAME,
  USER_NAME,
  ROLE_ID,
  SET_TOKEN,
  SET_ROLES_NAME
} from '@/store/mutation-types'
import utils from '@/utils/common'
var timeStamp = Number(new Date())
const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {},
    imgCode: '/captcha/captcha?captchaId=',
    adminId: '',
    userKey: '',
    loginName: '',
    userName: '',
    roleId: '',
    roleName: ''
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, {
      name,
      welcome
    }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    ADMIN_ID: (state, adminId) => {
      if (localStorage.getItem('adminId')) {
        state.adminId = localStorage.getItem('adminId')
      } else {
        state.adminId = adminId
      }
    },
    SET_ROLES_NAME: (state, roleName) => {
      state.roleName = roleName
    },
    USER_KEY: (state, userKey) => {
      if (localStorage.getItem('userKey')) {
        state.userKey = localStorage.getItem('userKey')
      } else {
        state.userKey = userKey
      }
    },
    LOGIN_NAME: (state, loginName) => {
      if (localStorage.getItem('loginName')) {
        state.loginName = localStorage.getItem('loginName')
      } else {
        state.loginName = loginName
      }
    },
    USER_NAME: (state, userName) => {
      if (localStorage.getItem('userName')) {
        state.userName = localStorage.getItem('userName')
      } else {
        state.userName = userName
      }
    },
    ROLE_ID: (state, roleId) => {
      if (localStorage.getItem('roleId')) {
        state.roleId = localStorage.getItem('roleId')
      } else {
        state.roleId = roleId
      }
    }
  },

  actions: {
    // 登录
    Login ({
      commit
    }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          // const result = response.result
          Vue.ls.set(ADMIN_ID, response.data.adminId, 7 * 24 * 60 * 60 * 1000)
          Vue.ls.set(USER_KEY, response.data.userKey, 7 * 24 * 60 * 60 * 1000)
          Vue.ls.set(LOGIN_NAME, response.data.loginName, 7 * 24 * 60 * 60 * 1000)
          Vue.ls.set(USER_NAME, response.data.userName, 7 * 24 * 60 * 60 * 1000)
          Vue.ls.set(ROLE_ID, response.data.roleId, 7 * 24 * 60 * 60 * 1000)
          Vue.ls.set(SET_TOKEN, response.data.adminId, 7 * 24 * 60 * 60 * 1000)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({
      commit
    }) {
      return new Promise((resolve, reject) => {
        const loginParams = {
          token: utils.AuthToken(timeStamp, Vue.ls.get(USER_KEY)),
          timestamp: timeStamp,
          adminId: Vue.ls.get(ADMIN_ID),
          id: Vue.ls.get(ROLE_ID)
        }
        getInfo(loginParams).then(response => {
          Vue.ls.set(SET_ROLES_NAME, response.data.name, 7 * 24 * 60 * 60 * 1000)
          commit('SET_ROLES', response.data.menuTree[0])
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({
      getters,
      state
    }) {
      return new Promise((resolve) => {
        const loginParams = {
          token: utils.AuthToken(timeStamp, getters.userKey),
          timestamp: timeStamp,
          adminId: state.adminId
        }
        logout(loginParams).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        }).finally(() => {
          Vue.ls.clear()
        })
      })
    },

    // 用户管理列表
    UserList ({
      getters
    }, data) {
      const loginParams = {
        token: utils.AuthToken(timeStamp, getters.userKey),
        adminId: getters.adminId,
        timestamp: timeStamp,
        keyword: data.keyword,
        province: data.province,
        flag: data.flag,
        startTime: data.startTime,
        endTime: data.endTime,
        page: data.page,
        pageSize: data.pageSize
      }
      return new Promise((resolve) => {
        userList(loginParams).then((response) => {
          resolve(response)
        }).catch((error) => {
          resolve(error)
        })
      })
    },
    // 用户管理状态编辑
    UserUpdate ({
      getters
    }, values) {
      const loginParams = {
        token: utils.AuthToken(timeStamp, getters.userKey),
        adminId: getters.adminId,
        timestamp: timeStamp,
        id: values.id,
        flag: values.flag
      }
      return new Promise((resolve, reject) => {
        userUpdate(loginParams).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
